package com.dldata.devtools30.utils;

import java.util.Calendar;
import java.util.Date;

public class EmumUtils {

    public static enum DATETIMEDEFAULT{

        上年初,本年初,本年上季度初,本年本季度初,上年本季度初,本年度上月初,本年度本月初,上年度本月初,
        当前天,前一天,后一天,
        上年尾,本年尾,本年上季度尾,本年本季度尾,上年本季度尾,本年度上月尾,本年度本月尾,上年度本月尾;


        public static String getValueFromItem(DATETIMEDEFAULT datetimedefault,String timeformatp){
            String timeformat = getFormatString(timeformatp);

            switch (datetimedefault){
                case 上年初:{
                    String datetime= DateUtil.formatFromDate("yyyy",new Date())+"-01-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)-1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());
                }
                case 本年初:{
                    String datetime= DateUtil.formatFromDate("yyyy",new Date())+"-01-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 本年上季度初:{
                    String datetime= DateUtil.formatFromDate("yyyy-MM",new Date())+"-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    int i = calendar.get(Calendar.MONTH) / 3;
                    i=i>0?i-1:i;
                    calendar.set(Calendar.MONTH, i *3);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 本年本季度初:{
                    String datetime= DateUtil.formatFromDate("yyyy-MM",new Date())+"-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)/3*3);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 上年本季度初:{
                    String datetime= DateUtil.formatFromDate("yyyy-MM",new Date())+"-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)-1);
                    calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)/3*3);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 本年度上月初:{
                    String datetime= DateUtil.formatFromDate("yyyy-MM",new Date())+"-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 本年度本月初:{
                    String datetime= DateUtil.formatFromDate("yyyy-MM",new Date())+"-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 上年度本月初:{
                    String datetime= DateUtil.formatFromDate("yyyy-MM",new Date())+"-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)-1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 上年尾:{
                    String datetime= DateUtil.formatFromDate("yyyy",new Date())+"-01-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)-1);
                    calendar.set(Calendar.SECOND,calendar.get(Calendar.SECOND)-1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 本年尾:{
                    String datetime= DateUtil.formatFromDate("yyyy",new Date())+"-01-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)+1);
                    calendar.set(Calendar.SECOND,calendar.get(Calendar.SECOND)-1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 本年上季度尾:{
                    String datetime= DateUtil.formatFromDate("yyyy-MM",new Date())+"-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    int i = calendar.get(Calendar.MONTH) / 3;
                    i=i>0?i-1:i;
                    calendar.set(Calendar.MONTH, i *3+3);
                    calendar.set(Calendar.SECOND,calendar.get(Calendar.SECOND)-1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 本年本季度尾:{
                    String datetime= DateUtil.formatFromDate("yyyy-MM",new Date())+"-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)/3*3+3);
                    calendar.set(Calendar.SECOND,calendar.get(Calendar.SECOND)-1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 上年本季度尾:{
                    String datetime= DateUtil.formatFromDate("yyyy-MM",new Date())+"-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)/3*3+3);
                    calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)-1);
                    calendar.set(Calendar.SECOND,calendar.get(Calendar.SECOND)-1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 本年度上月尾:{
                    String datetime= DateUtil.formatFromDate("yyyy-MM",new Date())+"-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    calendar.set(Calendar.SECOND,calendar.get(Calendar.SECOND)-1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 本年度本月尾:{
                    String datetime= DateUtil.formatFromDate("yyyy-MM",new Date())+"-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1);
                    calendar.set(Calendar.SECOND,calendar.get(Calendar.SECOND)-1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 上年度本月尾:{
                    String datetime= DateUtil.formatFromDate("yyyy-MM",new Date())+"-01";
                    Calendar calendar=Calendar.getInstance();
                    calendar.setTime(DateUtil.StringToDate(datetime,DateUtil.FORMATER_YYYY_MM_DD));
                    calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)-1);
                    calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1);
                    calendar.set(Calendar.SECOND,calendar.get(Calendar.SECOND)-1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 当前天:{
                    Calendar calendar=Calendar.getInstance();
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 前一天:{
                    Calendar calendar=Calendar.getInstance();
                    calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)-1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}
                case 后一天:{
                    Calendar calendar=Calendar.getInstance();
                    calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+1);
                    return DateUtil.formatFromDate(timeformat,calendar.getTime());}

            }
            return "";

        }


    }

    public static String getFormatString(String timeformatp) {
        String timeformat=DateUtil.FORMATER_YYYY_MM_DD;
        if("年".equals(timeformatp)){
            timeformat=DateUtil.YYYY;
        } else if("月".equals(timeformatp)){
            timeformat=DateUtil.YYYY_MM;
        }else if("日".equals(timeformatp)){
            timeformat=DateUtil.YYYY_MM_DD;
        }else if("时".equals(timeformatp)){
            timeformat=DateUtil.YYYY_MM_DD_HH;
        }else if("分".equals(timeformatp)){
            timeformat=DateUtil.YYYY_MM_DD_HH_MM;
        }else if("秒".equals(timeformatp)){
            timeformat=DateUtil.YYYY_MM_DD_HH_MM_SS;
        }
        return timeformat;
    }

}
